package ltu.m7011e.client;

import ltu.m7011e.client.event.GoToMainViewEvent;
import ltu.m7011e.client.event.GoToMainViewEventHandler;
import ltu.m7011e.client.event.PostLinkEvent;
import ltu.m7011e.client.event.PostLinkEventHandler;
import ltu.m7011e.client.presenter.MainPresenter;
import ltu.m7011e.client.presenter.PostLinkPresenter;
import ltu.m7011e.client.presenter.Presenter;
import ltu.m7011e.client.view.MainView;
import ltu.m7011e.client.view.PostLinkView;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HasWidgets;

public class AppController implements Presenter, ValueChangeHandler<String> {
	private final HandlerManager eventBus;
	private final LinkServiceRequester linkServiceRequester; 
	private HasWidgets container;

	public AppController(LinkServiceRequester linkRequester, HandlerManager eventBus) {
		this.eventBus = eventBus;
		this.linkServiceRequester = linkRequester;
		bind();
	}

	private void bind() {
		History.addValueChangeHandler(this);

		eventBus.addHandler(PostLinkEvent.TYPE,
				new PostLinkEventHandler() {
			public void onPostLinkEvent(PostLinkEvent event) {
				doPostLinkEvent();
			}
		});  
		
		eventBus.addHandler(GoToMainViewEvent.TYPE,
				new GoToMainViewEventHandler() {
			public void onClosePostBoxEvent(GoToMainViewEvent event) {
				doClosePostBoxEvent();
			}
		});  

		/*	    eventBus.addHandler(EditContactEvent.TYPE,
	        new EditContactEventHandler() {
	          public void onEditContact(EditContactEvent event) {
	            doEditContact(event.getId());
	          }
	        });  

	    eventBus.addHandler(EditContactCancelledEvent.TYPE,
	        new EditContactCancelledEventHandler() {
	          public void onEditContactCancelled(EditContactCancelledEvent event) {
	            doEditContactCancelled();
	          }
	        });  

	    eventBus.addHandler(ContactUpdatedEvent.TYPE,
	        new ContactUpdatedEventHandler() {
	          public void onContactUpdated(ContactUpdatedEvent event) {
	            doContactUpdated();
	          }
	        });  
		 */	    
	}

	private void doPostLinkEvent() {
		History.newItem("postLink");
		
		//linkServiceRequester.postLink();
	}
	
	private void doClosePostBoxEvent() {
		History.newItem("main");
		
		//linkServiceRequester.postLink();
	}

	/*	  private void doEditContact(String id) {
	    History.newItem("edit", false);
	    Presenter presenter = new EditContactPresenter(linkServiceRequester, eventBus, new EditContactView(), id);
	    presenter.go(container);
	  }

	  private void doEditContactCancelled() {
	    History.newItem("list");
	  }

	  private void doContactUpdated() {
	    History.newItem("list");
	  }
	 */	  
	public void go(final HasWidgets container) {
		this.container = container;

		if ("".equals(History.getToken())) {
			History.newItem("main");
		}
		else {		
			History.fireCurrentHistoryState();
		}
	}

	public void onValueChange(ValueChangeEvent<String> event) {
		String token = event.getValue();

		if (token != null) {
			Presenter presenter = null;

			if (token.equals("postLink")) {
				presenter = new PostLinkPresenter(linkServiceRequester, eventBus, new PostLinkView());
			}
			else if (token.equals("main")) {
				presenter = new MainPresenter(eventBus, new MainView());
				/*      }
	      else if (token.equals("edit")) {
	        presenter = new EditContactPresenter(linkServiceRequester, eventBus, new EditContactView());
	      }
				 */	      
				if (presenter != null) {
					presenter.go(container);
				}
			}
		} 
	}
}
